package com.tocici.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import com.tocici.domain.vo.CarouselVo;
import com.tocici.domain.bo.CarouselBo;
import com.tocici.service.ICarouselService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 轮播图
 *
 * @author zeratul
 * @date 2025-03-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@SaIgnore
@RequestMapping("/music/carousel")
public class CarouselController extends BaseController {

    private final ICarouselService carouselService;

    /**
     * 查询轮播图列表
     */
    @SaCheckPermission("music:carousel:list")
    @GetMapping("/list")
    public TableDataInfo<CarouselVo> list(CarouselBo bo, PageQuery pageQuery) {
        return carouselService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出轮播图列表
     */
    @SaCheckPermission("music:carousel:export")
    @Log(title = "轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CarouselBo bo, HttpServletResponse response) {
        List<CarouselVo> list = carouselService.queryList(bo);
        ExcelUtil.exportExcel(list, "轮播图", CarouselVo.class, response);
    }

    /**
     * 获取轮播图详细信息
     *
     * @param carouselId 主键
     */
    @SaCheckPermission("music:carousel:query")
    @GetMapping("/{carouselId}")
    public R<CarouselVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long carouselId) {
        return R.ok(carouselService.queryById(carouselId));
    }

    /**
     * 新增轮播图
     */
    @SaCheckPermission("music:carousel:add")
    @Log(title = "轮播图", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CarouselBo bo) {
        return toAjax(carouselService.insertByBo(bo));
    }

    /**
     * 修改轮播图
     */
    @SaCheckPermission("music:carousel:edit")
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CarouselBo bo) {
        return toAjax(carouselService.updateByBo(bo));
    }

    /**
     * 删除轮播图
     *
     * @param carouselIds 主键串
     */
    @SaCheckPermission("music:carousel:remove")
    @Log(title = "轮播图", businessType = BusinessType.DELETE)
    @DeleteMapping("/{carouselIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] carouselIds) {
        return toAjax(carouselService.deleteWithValidByIds(List.of(carouselIds), true));
    }
}
